package com.neu.controller;


import com.neu.pojo.User;
import com.neu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("addUser")
    @ResponseBody
    public HashMap addUser(User user){
        int res=userService.addUser(user);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "新建成功");
        }else{
            map.put("code", -1);
            map.put("msg", "新建失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("delUser")
    @ResponseBody
    public HashMap delUser(String userName){
        int res=userService.delUser(userName);
        HashMap<String, Object> map = new HashMap<>();
        if (res > 0) {
            map.put("code", 0);
            map.put("msg", "删除成功");
        } else {
            map.put("code", -1);
            map.put("msg", "删除失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("getUserByPage")
    @ResponseBody
    public HashMap getUserByPage(int page,int limit){
        List<User> list=userService.getUserByPage(page, limit);
        int totalCount=userService.getUserCount();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }

    @RequestMapping("updateUser")
    @ResponseBody
    public HashMap updateUser(User user){
        int res=userService.updateUser(user);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "更新成功");
        }else{
            map.put("code", -1);
            map.put("msg", "更新失败，请稍后重试.");
        }
        return map;
    }

}
